package com.haixin.security.handler;

import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 认证成功后，处理代码逻辑*/
@Slf4j
public class MyAuthenticationSuccessHandler implements AuthenticationSuccessHandler {
//登录成功后的访问地址
    private  String url;

    /*是否是重定向*/
    private Boolean isRedirect;

    public MyAuthenticationSuccessHandler(String url, Boolean isRedirect){
        this.url = url;
        this.isRedirect = isRedirect;
    }

    /**
     * 认证成功后，处理的具体逻辑
     *
     * authentication： 认证成功的用户主体对象。包含登录用户的个人信息和权限列表
     * */
    @Override
    public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException, ServletException {
        log.info("onAuthenticationSuccess执行");
        if (isRedirect){
            /*重定向*/
            response.sendRedirect(url);
        }else {
//            请求转发
            request.getRequestDispatcher(url).forward(request,response);
        }






    }
}
